package com.yml.mall.user;

import com.yml.mall.enums.ResponseEnum;
import com.yml.mall.enums.RoleEnum;
import com.yml.mall.user.pojo.LoginDTO;
import com.yml.mall.user.pojo.RegisterDTO;
import com.yml.mall.user.pojo.UserDO;
import com.yml.mall.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import static com.yml.mall.consts.myConst.CURRENT_USER;

/**
 * @ClassName UserController
 * @Author Administrator
 * @Date 2020/4/7   12:57
 */
@Api(tags = "用户api")
@Controller
@Slf4j
@Validated
public class UserController {
    private final UserService userService;
    @Autowired
    public UserController(UserServiceImpl userService1){
        this.userService=userService1;
    }
    @ApiOperation("注册")
    @PostMapping("/user/register")
    @ResponseBody
    public ResponseVO<UserDO> register(@RequestBody @Valid RegisterDTO registerDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("注册提交参数有误",bindingResult.getFieldError().getField()+":"+bindingResult.getFieldError().getDefaultMessage());
            return ResponseVO.error(ResponseEnum.FIELD_ERROR,bindingResult);
        }
        //注册时全部默认为普通用户
        registerDTO.setRole(RoleEnum.NORMAL_USER.getCode());

        return userService.register(registerDTO);
    }
    @ApiOperation("登录")
    @PostMapping("/user/login")
    @ResponseBody
    public ResponseVO login(@RequestBody  @Valid LoginDTO loginDTO, BindingResult bindingResult, HttpSession session){
        UserDO userDO=(UserDO) session.getAttribute(CURRENT_USER);
        if(userDO!=null && userDO.getUsername().equals(loginDTO.getUsername())){
            return ResponseVO.successNoData("重复登录");
        }
        ResponseVO responseVO=userService.login(loginDTO);
        //设置登录session
        session.setAttribute(CURRENT_USER,responseVO.getData());
        return responseVO;
    }
    @ApiOperation("用户信息")
    @GetMapping("/user")
    @ResponseBody
    public  ResponseVO user(@RequestBody String username,HttpSession session){
        if(session.getAttribute(CURRENT_USER)==null){
            return ResponseVO.error(ResponseEnum.NOT_LOGIN);
        }
        log.info(session.getAttribute(CURRENT_USER).toString());
        return  ResponseVO.success();
    }
    @ApiOperation("登出")
    @PostMapping("/user/logout")
    @ResponseBody
    public ResponseVO logOut(HttpSession session){
        if(session.getAttribute(CURRENT_USER)==null)
            return ResponseVO.error(ResponseEnum.NOT_LOGIN);
        session.removeAttribute(CURRENT_USER);
        return  ResponseVO.successNoData("登出成功");
    }

}
